package com.solcoo.merchant.util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.solcoo.common.util.ArrayUtil;
import com.solcoo.merchant.ApplicationExt;
import com.solcoo.merchant.R;

public class ViewUtil {

	public static final String TAG = "ViewUtil";

	public static void showToast(Context context, int res) {
		showToast(context, context.getResources().getString(res),
				Toast.LENGTH_SHORT);
	}

	public static void showToast(Context context, String msg) {
		showToast(context, msg, Toast.LENGTH_SHORT);
	}

	public static void showToast(Context context, String msg, int duration) {
		Context curActivity = ApplicationExt.getInstance().getCurrentContext();
		if (curActivity != context) {
			Log.w(TAG, "showToast is fail by context=" + context
					+ ",curActivity=" + curActivity);
			return;
		}

		View mToastNormalMsg = LayoutInflater.from(context).inflate(
				R.layout.util_toast_normal_msg, null);
		TextView mToastNormalMsgTxt = (TextView) mToastNormalMsg
				.findViewById(R.id.toast_msg_txt);
		mToastNormalMsgTxt.setText(msg);
		Toast toast = Toast.makeText(context, msg, duration);
		toast.setView(mToastNormalMsg);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	public static void showToastMsg(String fromActivity, String msg) {
		String curActivity = ApplicationExt.getInstance()
				.getCurrentContextName();
		if (curActivity.equals(fromActivity)) {
			ViewUtil.showToast(
					ApplicationExt.getInstance().getCurrentContext(), msg);
		} else {
			Log.w(TAG, "showToastMsg exec fail by curActivity=" + curActivity
					+ ",fromActivity=" + fromActivity);
		}
	}

	public static void setGoBackClickListener(View v) {
		if (v == null) {
			return;
		}
		v.setVisibility(View.VISIBLE);
		v.setOnClickListener(new OnGoBackClickListener());
	}

	private static class OnGoBackClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			((Activity) v.getContext()).finish();
		}
	};

	public static ProgressDialog createProgressDialog(Context context, int id) {
		return createProgressDialog(context, context.getString(id));
	}

	public static ProgressDialog createProgressDialog(Context context,
			String msg) {
		ProgressDialog pd = new ProgressDialog(context);
		pd.setMessage(msg);
		pd.setCanceledOnTouchOutside(false);
		return pd;
	}

	/**
	 * 设置当数据为空时显示页面
	 * 
	 * @param mLinearNoData
	 * @param activity
	 * @param exceptIds
	 */
	public static void showNotData(View mLinearNoData, Activity activity,
			int... exceptIds) {

		if (mLinearNoData == null) {
			Log.w(TAG, activity + " mLinearNoData view is null!!!");
			return;
		}
		if (activity == null) {
			Log.w(TAG, "activity is null!!!");
			return;
		}
		boolean showTitle = activity.findViewById(R.id.title_view) == null;
		if (showTitle) {
			mLinearNoData.findViewById(R.id.title_view).setVisibility(
					View.VISIBLE);
		}
		LayoutParams layoutParams = mLinearNoData.getLayoutParams();
		if (layoutParams != null) {
			if (layoutParams instanceof LinearLayout.LayoutParams) {
				LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layoutParams;
				lp.weight = Integer.MAX_VALUE;
			}
		}
		ViewGroup parent = (ViewGroup) mLinearNoData.getParent();
		final int length = parent.getChildCount();

		ArrayUtil.addAll(exceptIds, R.id.title_view);
		for (int i = 0; i < length; i++) {
			final View v = parent.getChildAt(i);
			if (ArrayUtil.contains(exceptIds, v.getId())) {
				v.setVisibility(View.GONE);
			}
		}
		mLinearNoData.setVisibility(View.VISIBLE);
	}
}
